/**
 * Created by kevin on 16/8/16.
 */
var eosi = window.eosi || {};
this.eosi.card = this.eosi.card || {};

(function (window) {
    // the application


    function CustomKeyboard(father,input) {

        this.Container_constructor();
        this.father = father;
        //this.input = input;
        this.init();
        //console.log(this.father.targetWidth);

    }


    var p = createjs.extend(CustomKeyboard, createjs.Container);
    p.father;
    p.inputStr="";
    p.h=200;//总高度
    p.w=200;//总高度
    p.row_max=3;//一行最多个数
    //p.input;
    //p.keyContainer

    p.drawKeyboard = function (){
        var w = this.father.targetWidth / (this.row_max+1);
        var h = w/3*2
        var count = 12;
        this.h = Math.ceil(count/this.row_max)*(h+1);
        this.w = this.row_max*(w+1);
        //console.log(w);
        for(var i =0 ;i<count;i++){
            //var keyContainer = new createjs.Container();
            var key = new createjs.Shape();

            key.addEventListener("pressup",this.clickKey);
            key.alpha = .8;
            key.graphics.beginFill("#ffffff").drawRoundRect(0,0,w,h,10);
            this.addChild(key);
            key.cache(0,0,w,h);
            //shape.cache(-radius, -radius, radius * 2, radius * 2);

            var num;
            if(i<9){
                num = i+1 ;
            }
            if(i==9){
                num = "OK";
            }
            if(i==10){
                num = 0;
            }
            if(i==11){
                num="Del";
            }

            key.num = num;
            var txt = new createjs.Text(num, "100px Arial", "#000");
            txt.alpha = .5;
            txt.textAlign="left";
            this.addChild(txt);
            key.x = (w+1)*(i%this.row_max);
            key.y = (h+1)*(Math.floor(i/this.row_max));

            if(i!=11 && i!=9){
                txt.x = (w+1)*(i%3)+(txt.getMeasuredWidth())/2;
            }else{
                txt.x = (w+1)*(i%3);
            }

            txt.y = (h+1)*(Math.floor(i/3));
            //console.log(txt.getMeasuredWidth());

        }
    }
    p.clickKey = function(evt){
        //console.log(evt.target.num)
        //dispatchEvent("num");
        var key = evt.target;
        //createjs.Tween.get(moon)
        //    .to({alpha:.8}, 200)
        //    .to({alpha:.5}, 200)
        createjs.Tween.get(key)
            .to({alpha:1}, 300).call(keyStatusBack)
            //.to({alpha:.5}, 10)
        function keyStatusBack(){
            key.alpha = .8;
        }

        if(!isNaN(evt.target.num) && evt.target.parent.inputStr.length < 11){
            evt.target.parent.inputStr += evt.target.num;
        }
        if(evt.target.num == "Del"){

            evt.target.parent.inputStr=evt.target.parent.inputStr.substring(0,evt.target.parent.inputStr.length-1)
        }
        if(evt.target.num == "OK"){

            evt.target.parent.father.hideInput();
            return;
        }

        evt.target.parent.father.input.text = evt.target.parent.inputStr;
        //console.log(evt.target.parent.inputStr)
        //this.input.text = evt.target.num;
    }

    // public methods:
    p.init = function () {
        console.log("I'm keyboard");
        this.drawKeyboard();



    }

    // add MyApp to myNameSpace
    window.eosi.card.CustomKeyboard = createjs.promote(CustomKeyboard, "Container");
//}());
}(window));

